package cn.mineserv.video.videoordersystem.utils;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @author zhang hongzhi
 * num    19001021020
 * class  19010
 */
public class CookieUtil {

    public static String getValue(HttpServletRequest request, String name) {
        if (request == null || name == null) {
            throw new IllegalArgumentException("参数为空!不存在");
        }

        Cookie[] cookies = request.getCookies();
        if (cookies != null) {
            for (Cookie cookie : cookies) {
                if (cookie.getName().equals(name)) {
                    return cookie.getValue();
                }
            }
        }
        return null;
    }

    public static void setValue(HttpServletResponse response, String name, String value) {
        if (response == null || name == null || value == null) {
            throw new IllegalArgumentException("参数为空!不存在");
        }
        Cookie cookie = new Cookie(name, value);
        cookie.setMaxAge(60 * 60 * 24 * 7);
        cookie.setPath("/VideoOrderSystem");
        response.addCookie(cookie);
    }

    public static void removeValue(HttpServletResponse response, String name) {
        if (response == null || name == null) {
            throw new IllegalArgumentException("参数为空!不存在");
        }
        Cookie cookie = new Cookie(name, null);
        cookie.setMaxAge(0);
        response.addCookie(cookie);
    }

}
